﻿using System.Linq;
using HappyBa.Core.Data;
using HappyBa.Core.Interfaces;
using HappyBa.Core.Reporting;
using HappyBa.Core.Statistics;
using HappyBa.Core.TasksModification;
using HappyBa.Core.UserReport;
using HappyBa.Core.Validators;
using LightInject;

namespace HappyBa.Core
{
    public static class ServicesRegistrar
    {
        public static void Register(ServiceContainer container)
        {
            RegisterCollection<IValidator>(container);
            RegisterCollection<IStatisticsCalculator>(container);
            container.Register<ITargetProccessFacade, TargetProccessFacade>(new PerContainerLifetime());
            container.Register<IDataReceiver, DataReceiver>(new PerContainerLifetime());
            container.Register<IDataCache, DataCache>(new PerContainerLifetime());
            container.Register<IValidationFacade, ValidationFacade>(new PerContainerLifetime());
            container.Register<IStatisticsFacade, StatisticsFacade>(new PerContainerLifetime());
            container.Register<IFullReportGenerator, FullReportGenerator>(new PerContainerLifetime());
            container.Register<ITasksManager, TasksManager>(new PerContainerLifetime());
            container.Register<IValidationReportGenerator, ExcelValidationReportGenerator>(new PerContainerLifetime());
            container.Register<IUserStatisticsCalculator, UserStatisticsCalculator>(new PerContainerLifetime());
            container.Register<IUsersReportGenerator, UsersReportGenerator>(new PerContainerLifetime());
        }

        private static void RegisterCollection<T>(ServiceContainer container)
        {
            var type = typeof(T);
            foreach (var t in type.Assembly.GetTypes().Where(x => x.GetInterfaces().Contains(type)))
            {
                container.Register(type, t, t.Name, new PerContainerLifetime());
            }
        }
    }
}
